package com.onlyphoto.onlymall.utils;


import androidx.annotation.UiThread;
import androidx.fragment.app.FragmentTransaction;

import com.onlyphoto.onlymall.MyApplication;
import com.onlyphoto.onlymall.base.BaseDialogFragment;
import com.onlyphoto.onlymall.widget.BaseDialogFragmentWrapper;

import java.util.Comparator;
import java.util.Queue;
import java.util.concurrent.PriorityBlockingQueue;

/**
 * Created by gaochujia on 2019-09-19.
 */

public class DialogFragmentManager {
    private String TAG = DialogFragmentManager.class.getSimpleName();
    Comparator<BaseDialogFragmentWrapper> OrderIsdn = (o1, o2) -> {
        int numberA = o1.getPriority();
        int numberB = o2.getPriority();
        if (numberB < numberA) {
            return 1;
        } else if (numberB > numberA) {
            return -1;
        } else {
            long timeA = o1.getTimestamp();
            long timeB = o2.getTimestamp();
            if (timeA > timeB) {
                return 1;
            } else if (timeA < timeB) {
                return -1;
            }
            return 0;
        }

    };
    Queue<BaseDialogFragmentWrapper> priorityQueue = new PriorityBlockingQueue<>(10, OrderIsdn);
    private static volatile DialogFragmentManager instance = null;

    private boolean hasDialogShowing = false;
    private BaseDialogFragmentWrapper cache;

    private DialogFragmentManager() {
    }

    public static DialogFragmentManager obtain() {
        if (null == instance) {
            synchronized (DialogFragmentManager.class) {
                if (null == instance) {
                    instance = new DialogFragmentManager();
                }
            }
        }
        return instance;
    }

    private BaseDialogFragment.DismissCallBack dismissCallBack = () -> {
        hasDialogShowing = false;
        check();
    };

    @UiThread
    public void addDialog(BaseDialogFragmentWrapper baseDialogFragmentWrapper) {
        if (null == baseDialogFragmentWrapper) {
            return;
        }
        baseDialogFragmentWrapper.getBaseDialogFragment().setDismissCallBack(dismissCallBack);
        if (baseDialogFragmentWrapper.isForceCloseOther() && hasDialogShowing) {//如果设置了强制关闭别的弹窗
            if (null != cache) {
                cache.getBaseDialogFragment().dismiss();
                hasDialogShowing = false;
                cache = null;
            }
        }
        priorityQueue.add(baseDialogFragmentWrapper);
        check();
    }

    private void check() {
        if (!priorityQueue.isEmpty()) {
            if (!hasDialogShowing) {
                cache = priorityQueue.poll();
                if (null != cache && null != cache.getBaseDialogFragment() && null != MyApplication.app.getCurActivity()) {
                    FragmentTransaction transaction = MyApplication.app.getCurActivity().getSupportFragmentManager().beginTransaction();
                    transaction.add(cache.getBaseDialogFragment(), cache.getTag());
                    transaction.commitAllowingStateLoss();
                    hasDialogShowing = true;
                }
                if (!priorityQueue.isEmpty()) {
                    check();
                }
            }
        }

    }
}
